#!/usr/bin/env python

import sys, os, Cookie

class CookieManager ( object ):
	def __init__ ( self, pyhp ):
		self._cookies = Cookie.SimpleCookie ()
		self._pyhp = pyhp

		self.log = pyhp.log.getLogger ( "CookieMan" )

		self.defaults = {
			'expires' : '',
			'path' : '/',
			'comment' : '',
			'domain' : '',
			'max-age' : '',
			'secure' : '',
			'version' : '',
			'httponly' : ''
		}

	def create ( self, name, value ):
		self._cookies [ name ] = value
		
		for k, v in self.defaults.iteritems ():
			if v:
				self._cookies [ name ]  [ k ] = v

	def dump ( self ):
		res = []
		
		for cookie in self._cookies.values():
			res.append ( ( ( str ( cookie ).split ( ":", 1 ) [ 1 ] ).strip () ) )

		return res

	def read ( self, str_input ):
		self._cookies.load ( str_input )

	def set ( self, cookie_name, attr, value ):
		self._cookies [ cookie_name ] [ attr ] = value

	def get ( self, cookie_name ):
		return self._cookies.get ( cookie_name )

	def drop ( self, cookie_name ):
		self._cookies [ cookie_name ] [ 'expires' ] = -1

